package com.zhugang.week05;

import java.util.Arrays;

/**
 * @program algorithms
 * @description: checkPermutation
 * @author: chanzhugang
 * @create: 2022/07/08 15:44
 */
public class CheckPermutation {

    /**
     * 面试题 01.02 判定是否互为字符重排
     *
     * @param s1
     * @param s2
     * @return
     */
    public boolean checkPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        // 字符串转字符数组，排序， 比较每个字符
        char[] array1 = s1.toCharArray();
        char[] array2 = s2.toCharArray();
        Arrays.sort(array1);
        Arrays.sort(array2);
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    }
}